#include "apps.h"
#include "esp_log.h"

void rfid_init()
{
    reader.begin(SDA, SCL);
    // 替换 Serial.print 和 Serial.println
    uint8_t version = reader.getVersion();
    ESP_LOGI("RFID", "CLRC663 version: %d", version);
}

// Hex print for blocks without printf.
void print_block(uint8_t * block,uint8_t length){
    char hex_str[3 * length + 1]; // 每个字节最多 2 个字符和 1 个空格，最后留一个字符给字符串结束符
    hex_str[0] = '\0';
    for (uint8_t i = 0; i < length; i++){
        // Increase the size of the temp buffer
        char temp[5]; 
        snprintf(temp, sizeof(temp), "%02X ", block[i]);
        strcat(hex_str, temp);
    }
    ESP_LOGI("RFID", "%s", hex_str);
}

uint8_t uid[10] = {0}; // variable for 10byte UID
bool cardFound = false;
uint8_t iValue;
uint8_t qValue;
uint8_t irq1;
uint8_t i_result;
uint8_t q_result;
static uint32_t last = 0;
uint8_t uid_len ;
uint8_t password[] = {0x0F, 0x0F, 0x0F, 0x0F};


void detect()
{
    cardFound = false;
    reader.softReset();
    // Set the registers of the CLRC633 into the default for ISO-14443
    reader.AN1102_recommended_registers(MFRC630_PROTO_ISO14443A_106_MILLER_MANCHESTER);
    uid_len = reader.read_iso14443_uid(uid);

    if (uid_len != 0)
    { // did we get an UID?
        // 替换 Serial.print
        ESP_LOGI("RFID", "ISO-14443 tag found! UID of %d bytes: ", uid_len);
        print_block(uid, uid_len);
    }

    reader.softReset();
    reader.AN1102_recommended_registers(MFRC630_PROTO_ISO15693_1_OF_4_SSC);

    uid_len = reader.read_iso18693_uid(uid, password);
    if (uid_len != 0)
    {
        // 替换 Serial.print
        ESP_LOGI("RFID", "ISO-18693 tag found! UID of %d bytes: ", uid_len);
        print_block(uid, uid_len);
    }
    // no card found!
    //

    // measure LPCD

    // reader.AN11145_start_IQ_measurement(&iValue, &qValue);
    // // 替换 Serial.println 和 Serial.print
    // ESP_LOGI("RFID", "IQ_measurement:");
    // ESP_LOGI("RFID", "in-phase (I): %d", iValue);
    // ESP_LOGI("RFID", "quadrature (Q): %d", qValue);

    // // go to LPCD mode
    // ESP_LOGI("RFID", "goto sleep..");
    // reader.softReset();
    // reader.lpcd_start(iValue, qValue);
}
bool wait_LPCD()
{
    if (digitalRead(IRQ_PIN) == LOW)
    {
        // 替换 Serial.println
        ESP_LOGI("RFID", "wakeup!");
        irq1 = reader.get_irq1();
        if ((irq1 & MFRC630_IRQ1_LPCD_IRQ) > 0)
            ESP_LOGI("RFID", "LPCD IRQ!");
        i_result = reader.read_reg(MFRC630_REG_LPCD_I_RESULT);
        q_result = reader.read_reg(MFRC630_REG_LPCD_Q_RESULT);

        // 替换 Serial.print 和 Serial.println
        ESP_LOGI("RFID", "in-phase (I): %d", i_result);
        ESP_LOGI("RFID", "quadrature (Q): %d", q_result);
        return true;
    }
    return false;
}
void rfid_loop()
{
    static int state = 0;

    if(millis() - last < 100)
    {
        return;
    }
    last = millis();
    switch (state)
    {
    case 0:
        detect();
        state = 0;
        break;
    case 1:
        // wait for LPCD wakeup-IRQ
        if (wait_LPCD())
        {
            state = 0;
        }
        break;
    default:
        break;
    }
}